package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderDetailVO;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.websocket.WebSocketServer;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "用户下单相关接口")
public class OrderController {



    @Autowired
   private OrderService orderService;

    @Autowired
    private WebSocketServer webSocketServer;

    /**
     * 用户下单功能
     * @param ordersSubmitDTO
     * @return
     */
    @ApiOperation("用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> OrderSubmit(@RequestBody OrdersSubmitDTO ordersSubmitDTO) {
        log.info("用户下单：{}", ordersSubmitDTO);
        OrderSubmitVO orderSubmitVO = orderService.submitOrder(ordersSubmitDTO);
        log.info("返回的数据：{}", orderSubmitVO);
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        orderService.paySuccess(ordersPaymentDTO.getOrderNumber());
        return Result.success(orderPaymentVO);
    }


    /**
     * 查询订单详情
     * @return Result.success(orderDetail);
     */
    @GetMapping("/orderDetail/{id}")
    @ApiOperation("查询订单详情")
    public Result<OrderDetailVO> orderDetail(@PathVariable String id) {
        log.info("查询订单详情{}",id);
        OrderDetailVO orderDetail = orderService.getOrderDetailById(id);
        log.info("订单详情{}",orderDetail);
        return Result.success(orderDetail);
    }

    /**
     * 查询历史订单
     * @param ordersPageQueryDTO
     * @return Result.success(pageResult);
     */
    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO) {
        log.info("查询历史订单DTO{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.getHistoryOrder(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("cancel/{id}")
    @ApiOperation("取消订单")
    public Result cancelOrder(@PathVariable String id){
        log.info("取消订单{}",id);
        orderService.cancelOrderById(id);
        return Result.success();
    }

    /**
     * 催单功能
     * @param id
     * @return
     */
    @GetMapping("reminder/{id}")
    @ApiOperation("催单功能")
    public Result reminderOrder(@PathVariable String id){
        log.info("订单为{}发起催单了！",id);
        //催单功能
        orderService.reminder(id);
        return Result.success();
    }

    /**
     * //再来一单是把订单数据再加到购物车里面
     * @param id
     * @return
     */
    @PostMapping("repetition/{id}")
    @ApiOperation("再来一单")
    public Result repetitionOrder(@PathVariable String id){
        log.info("再来一单{}",id);
        //再来一单是把订单数据再加到购物车里面
        orderService.repetitionOrder(id);
        return Result.success();
    }

}
